﻿/*using System;
using System.Collections.Specialized;
using System.Reflection;
using System.Web;
using System.Web.Services;

namespace OverlakeServer_SOAP
{
    class HttpCompressionModule : IHttpModule
    {
        public void Dispose()
        {

        }

        public void Init(HttpApplication app)
        {
            app.PreRequestHandlerExecute += new EventHandler(app_PreRequestHandlerExecute);
        }

        bool shouldCompress = false;

        void app_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            try
            {
                //HttpApplication app = sender as HttpApplication;
                //app.Request.Headers["Accept-encoding"] = "gzip";



                HttpApplication objApp = (HttpApplication)sender;
                HttpRequest Request = (HttpRequest)objApp.Context.Request;


                //get a reference
                NameValueCollection headers = Request.Headers;

                //get a type
                Type t = headers.GetType();
                System.Collections.ArrayList item = new System.Collections.ArrayList();

                t.InvokeMember("MakeReadWrite", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, headers, null);
                t.InvokeMember("InvalidateCachedArrays", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, headers, null);
                
                if (shouldCompress)
                {
                    item.Add("gzip");
                }
                else
                {
                    item.Add("identity");
                }

                t.InvokeMember("BaseAdd", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, headers, new object[] { "Accept-Encoding", item });
                t.InvokeMember("MakeReadOnly", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, headers, null); 
            }
            catch { }
        }
    }
}*/
#pragma warning disable

using System;
using System.IO.Compression;
using System.Web;
using System.Web.Security;

namespace OverlakeServer_SOAP
{
    public class HttpCompressionModule : IHttpModule
    {
        private bool _isDisposed = false;

        public void Init(HttpApplication context)
        {
            context.BeginRequest += context_BeginRequest;
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            var app = sender as HttpApplication;
            var ctx = app.Context;

            app.Response.Headers.Add("TESTHEADER", "HELLOWORLD");

            if (!ctx.Request.Url.PathAndQuery.ToLower().Contains(".asmx"))
                return;

            if (IsEncodingAccepted("gzip"))
            {
                app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
                SetEncoding("gzip", app);
            }
            else if (IsEncodingAccepted("deflate"))
            {
                app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
                SetEncoding("deflate", app);
            }
        }
        private bool IsEncodingAccepted(string encoding)
        {
            return HttpContext.Current.Request.Headers["Accept-encoding"] != null && HttpContext.Current.Request.Headers["Accept-encoding"].Contains(encoding);
        }
        private void SetEncoding(string encoding, HttpApplication app)
        {
            app.Response.Headers["Content-Encoding"] = encoding;
            //HttpContext.Current.Response.AppendHeader("Content-encoding", encoding);
        }
        private void Dispose(bool dispose)
        {
            _isDisposed = dispose;
        }
        ~HttpCompressionModule()
        {
            Dispose(false);
        }
        public void Dispose()
        {
            Dispose(true);
        }
    }
}
#pragma warning enable